﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WineStore.DAL;
using WineStore.Models;
using System.Data;
using PagedList;
using PagedList.Mvc;
using System.Data.Entity;

namespace WineStore.Controllers
{

    public class OrderSimController : Controller
    {
        // GET: /OrderSim/ShowOrderSim/
        public ViewResult ShowOrderSim(int? page)
        {
            using (var db = new WineDbContext())
            {
                var ordersims = db.OrderSims.Include(o => o.Sim).Include(o=>o.Sim.Supplier).Include(o=>o.Sim.CategorySim).Where(o => o.Status == 1).OrderByDescending(o => o.OrderSimId);
                int pageSize = 50;
                int pageNumber = (page ?? 1);

                // Thiet lap thuoc tinh khi phan trang
                PagedListRenderOptions pro = new PagedListRenderOptions();
                pro.MaximumPageNumbersToDisplay = 10;
                pro.LinkToLastPageFormat = "Last";
                pro.DisplayLinkToLastPage = true;
                pro.LinkToFirstPageFormat = "First";
                pro.DisplayLinkToFirstPage = true;
                pro.LinkToNextPageFormat = ">>";
                pro.DisplayLinkToNextPage = true;
                pro.LinkToPreviousPageFormat = "<<";
                pro.DisplayLinkToPreviousPage = true;

                ViewBag.Pro = pro;

                return View(ordersims.ToPagedList(pageNumber, pageSize));
            }
        }

        // GET: /OrderSim/ShowOrderSimByStatus/
        public ViewResult ShowOrderSimByStatus(int? status, int? page)
        {
            ViewBag.Status = status;
            ViewBag.StatusDes = null;
            if (status == 1)
            {
                ViewBag.StatusDes = "Chưa xử lý";
            }
            else if (status == 2)
            {
                ViewBag.StatusDes = "Đã xử lý";
            }

            using (var db = new WineDbContext())
            {
                var ordersims = db.OrderSims.Include(o => o.Sim).Include(o => o.Sim.Supplier).Include(o => o.Sim.CategorySim).Where(o => o.Status == status).OrderByDescending(o => o.OrderSimId);
                int pageSize = 50;
                int pageNumber = (page ?? 1);

                // Thiet lap thuoc tinh khi phan trang
                PagedListRenderOptions pro = new PagedListRenderOptions();
                pro.MaximumPageNumbersToDisplay = 10;
                pro.LinkToLastPageFormat = "Last";
                pro.DisplayLinkToLastPage = true;
                pro.LinkToFirstPageFormat = "First";
                pro.DisplayLinkToFirstPage = true;
                pro.LinkToNextPageFormat = ">>";
                pro.DisplayLinkToNextPage = true;
                pro.LinkToPreviousPageFormat = "<<";
                pro.DisplayLinkToPreviousPage = true;

                ViewBag.Pro = pro;

                return View(ordersims.ToPagedList(pageNumber, pageSize));
            }
        }

        // GET: /OrderSims/DeleteOrderSims/
        public ActionResult DeleteOrderSim(int id)
        {
            OrderSim ordersim = null;
            using (var db = new WineDbContext())
            {
                ordersim = db.OrderSims.Find(id);
                ViewBag.SimNumber = ordersim.Sim.SimNumber;
                if (ordersim == null)
                {
                    return HttpNotFound();
                }
            }
            return View(ordersim);
        }

        // POST: /OrderSims/DeleteOrderSims/
        [HttpPost, ActionName("DeleteOrderSim")]
        public ActionResult DeleteConfirmed(int id)
        {
            OrderSim ordersim = null;
            try
            {
                using (var db = new WineDbContext())
                {
                    ordersim = db.OrderSims.Find(id);
                    db.Entry(ordersim).State = EntityState.Deleted;
                    db.SaveChanges();
                    return RedirectToAction("ShowOrderSim", "OrderSim");
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes");
            }
            return View(ordersim);
        }

        // GET: /OrderSim/ChangeStatus/
        public ActionResult ChangeStatus(int id, int status)
        {
            OrderSim ordersim = null;
            try
            {
                using (var db = new WineDbContext())
                {
                    ordersim = db.OrderSims.Find(id);
                    ordersim.Status = status + 1;
                    db.Entry(ordersim).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("ShowOrderSimByStatus", "OrderSim", new { status = status });
                }

            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes");
            }
            return View(ordersim);
        }


        // GET: /OrderSim/ActiveOrderSim/
        public ActionResult ActiveOrderSim(int id)
        {
            OrderSim ordersim = null;
            try
            {
                using (var db = new WineDbContext())
                {
                    ordersim = db.OrderSims.Find(id);
                    ordersim.Status = 2;
                    db.Entry(ordersim).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("ShowOrderSim", "OrderSim");
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes");
            }
            return View(ordersim);
        }

    }
}
